package edu.egypt.contactread;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ContactReadSampleActivity extends Activity {
	
    private Button button;
	private EditText editText;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button = (Button)findViewById(R.id.button1);
        editText = (EditText)findViewById(R.id.editText1);
        
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				   Uri contactsURI = Uri.parse("content://contacts/people");
			        
			        String[] projection = new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
			        Cursor c = managedQuery(contactsURI, projection, null, null, null);
			        c.moveToFirst();
			        while(!c.isAfterLast()){
			        	String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));	
			        	String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			        	editText.append(id+","+name+"\n");
			        	c.moveToNext();
			        }
			}
		});
        
        
     
    }
}